require File.dirname(__FILE__) + '/customer.rb'
require File.dirname(__FILE__) + '/administrator.rb'
require File.dirname(__FILE__) + '/read_write.rb'
require File.dirname(__FILE__) + '/employee.rb'
require File.dirname(__FILE__) + '/rezervation.rb'
require File.dirname(__FILE__) + '/room.rb'
require 'rubygems'
require 'highline/import'
require 'digest/md5'
require 'yaml'

#kliento registracija
def customer_registration()
  
  customers=read_customers;
  if customers==nil
    customerIndex=0
    customers=[]
  else
    customerIndex=customers[customers.length-1].id+1
  end
  puts "Iveskite prisijungimo varda:"
  login = gets
  puts "Iveskite varda:"
  fname = gets
  puts "Iveskite pavarde:"
  lname = gets
  pwcmp1 = Digest::MD5.hexdigest(ask("Iveskite slaptazodi:\n") { |q| q.echo = false })
  pwcmp2= Digest::MD5.hexdigest(ask("Pakartokite slaptazodi:\n") { |q| q.echo = false })
  if !does_customer_login_not_exist(login, customers)
    puts "Toks prisijungimo vardas jau uzimtas"
    return false
  end
  if pwcmp1 == pwcmp2
    puts "Registracija pavyko!\n"
    customers[customerIndex]=Customer.new(customerIndex,login, fname, lname, pwcmp1)
    write_customers(customers)
  else
    puts "Regitracija nepavyko! Slaptazodziai nesutampa!!\n"
    return false
  end
end

#kliento prisijungimas
def customer_login(customers)
  puts "Iveskite prisijungimo varda: "
  login = gets
  password = Digest::MD5.hexdigest(ask("Iveskite slaptazodi:\n") { |q| q.echo = false })
  customer=customer_login_check(login, password, customers)
  if customer==false
    puts "Neteisingas vartotojo vardas arba slaptazodis"
  else
    customer_menu(customer)
  end
end

#prisijungusio kliento meniu
def customer_menu(customer)
  puts "**************************\nCustomer menu(kolkas nera)\n**************************"
end

#darbuotojo prisijungimas
def employee_login
  employees=read_employees
  puts "Iveskite prisijungimo varda: "
  login = gets
  password = Digest::MD5.hexdigest(ask("Iveskite slaptazodi:\n") { |q| q.echo = false })
  employee=employee_login_check(login, password, employees)
  if employee==false
    puts "Neteisingas vartotojo vardas arba slaptazodis"
  else
    employee_menu(employee)
  end
end
#darbuotojo registracija
def employee_registration
  
  employees=read_employees;
  if employees==nil
    employeeIndex=0
    employees=[]
  else
    employeeIndex=employees[employees.length-1].id+1
  end
  puts "Iveskite prisijungimo varda:"
  login = gets
  puts "Iveskite varda:"
  fname = gets
  puts "Iveskite pavarde:"
  lname = gets
  pwcmp1 = Digest::MD5.hexdigest(ask("Iveskite slaptazodi:\n") { |q| q.echo = false })
  pwcmp2= Digest::MD5.hexdigest(ask("Pakartokite slaptazodi:\n") { |q| q.echo = false })
  if !does_employee_login_not_exist(login, employees)
    puts "Toks prisijungimo vardas jau uzimtas"
    return false
  end
  if pwcmp1 == pwcmp2
    puts "Registracija pavyko!\n"
    employees[employeeIndex]=Employee.new(employeeIndex,login, fname, lname, pwcmp1)
    write_employees(employees)
  else
    puts "Regitracija nepavyko! Slaptazodziai nesutampa!!\n"
    return false
  end
end

def employee_menu(customer)
  puts "**************************\nEmployee menu(kolkas nera)\n**************************"
end
#administratoriaus prisijungimas
def administrator_login()
  administrators=read_adaministrators
  puts "Iveskite prisijungimo varda: "
  login = gets
  password = Digest::MD5.hexdigest(ask("Iveskite slaptazodi:\n") { |q| q.echo = false })
  administrator=administrator_login_check(login, password, administrators)
  if administrator==false
    puts "Neteisingas vartotojo vardas arba slaptazodis"
  else
    administrator_menu(administrator)
  end
end

#prisijungusio administratoriaus meniu
def administrator_menu(administrator)
  while true
    puts 
    puts "****************Meniu***************"
    puts "1 - Darbuotojo registracija"
    puts "2 - Prideti kambari"
    puts 
    puts "0 - atsijungti" 
    puts "************************************"
    puts "Iveskite komanda:"
    command = gets
    if command == "1\n"
      employee_registration
    elsif command=="2\n"
      room_registration_menu
    elsif command == "0\n"
      break
    else 
      puts "Ivedete neteisinga komanda"
    end
  end
end
#kambario registracijos meniu
def room_registration_menu
  puts
  puts "Iveskite kambario numeri:"
  number = gets
  puts "Iveskite kambario kaina"
  price = gets
  puts "Iveskite kambario talpa"
  capasity = gets
  puts
  if add_room(number, price, capasity)
    puts "Registracija pavyko"
  else
    puts "Registracija nepavyko"
end
  
#pagrintinis meniu

while true
  puts 
  puts "****************Meniu***************"
  puts "1 - Kliento registracija"
  puts "2 - Kliento prisijungimas"
  puts "3 - Darbuotojjo prisijungimas"
  puts "4 - Administratoriaus prisijungimas"
  puts
  puts "0 - baigti darba" 
  puts "************************************"
  puts "Iveskite komanda:"
  command = gets
  if command == "1\n"
    customer_registration()
  elsif command == "2\n"
    customer_login()
  elsif command == "3\n"
    employee_login
  elsif command == "4\n"
    administrator_login()
  elsif command == "0\n"
    break
  else 
    puts "Ivedete neteisinga komanda!!!"
  end
end
